package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Supplier;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService service;
    @RequiredPermission("供应商列表")
    @RequestMapping("list")
    public String list(Map map, QueryObject qo) {
        PageResult pageResult = service.query(qo);
        map.put("pageResult", pageResult);
        return "supplier/list";
    }
    @RequiredPermission("供应商编辑")
    @RequestMapping("input")
    public String input(Map map, Long id) {
        if (id != null) {
            Supplier supplier = service.get(id);
            map.put("supplier", supplier);
        }
        return "supplier/input";
    }
    @RequiredPermission("供应商保存或更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Supplier supplier) {
        if (supplier.getId() != null) {
            try {
                service.update(supplier);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        } else {
            try {
                service.save(supplier);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("操作成功");
    }
    @RequiredPermission("供应商删除")
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            try {
                service.delete(id);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("删除成功");
    }
}
